package com.tj.admin.rest.product;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.qianli.cashmama.product.facade.ProductFacade;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.tj.admin.auth.anno.VisitorAccessible;
import com.tj.admin.auth.request.AddProductRequest;
import com.tj.admin.auth.request.EditorProductRequest;
import com.tj.admin.auth.response.ProductListDTO;
import com.tj.admin.auth.response.ProductListResponse;
import com.tj.admin.auth.service.ProductApplication;
import com.tj.admin.auth.vo.RequestLocalInfo;
import com.tj.admin.auth.vo.UserAccessInfo;
import com.tj.bo.common.conf.api.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductFacade productFacade;

    @RequestMapping(value = "/list")
    public ApiResponse<ProductListResponse> list() {
        String merchantCode = RequestLocalInfo.getCurrentUser().getMerchantCode();
        Response<List<ProductCategory>> productCategories = productFacade.getProductCategoriesByMerchantCode(merchantCode);
        if (productCategories == null || productCategories.getData() == null || CollectionUtils.isEmpty(productCategories.getData())) {
            return new ApiResponse<>(new ProductListResponse());
        }

        return new ApiResponse<>(new ProductListResponse(productCategories.getData(), 0));
    }


    @RequestMapping(value = "/update")
    public ApiResponse update(@RequestBody ProductUpdateRequest request) {
        Response<ProductCategory> categoryByCode = productFacade.getCategoryByCode(request.getCode());
        if (categoryByCode == null || categoryByCode.getData() == null) {
            return new ApiResponse(601, "category not exist");
        }

        ProductCategory productCategory = new ProductCategory()
                .setCode(request.getCode())
                .setIsSelling(request.getSelling())
                .setDailyApplyLimit(request.getDayLimit());


        String extraInfo = categoryByCode.getData().getExtraInfo();
        Map<String, Object> temp = new HashMap<>();
        if (StringUtils.isNotEmpty(extraInfo)) {
            Map<String, Any> stringAnyMap = JsonIterator.deserialize(extraInfo).asMap();
            for (String key : stringAnyMap.keySet()) {
                temp.put(key, stringAnyMap.get(key).toString());
            }
        }
        if (request.getLendingUserType() != null) {
            temp.put("lendingUserType", request.getLendingUserType());
        }

        extraInfo = JsonStream.serialize(temp);
        productCategory.setExtraInfo(extraInfo);
        productFacade.updateProductCategoryByCode(productCategory);

        return new ApiResponse();
    }


}
